Import librairies

In [25]:
import yfinance as yf
import pandas as pd
import csv
from tqdm.notebook import tqdm
import html5lib
import plotly.express as px
from datetime import datetime
import plotly.graph_objects as go
import plotly.figure_factory as ff
from plotly.subplots import make_subplots
import numpy as np
import plotly.offline as pyo
from scipy.stats import linregress, norm

pyo.init_notebook_mode()
pd.set_option("display.max_columns", 101)

ESSAY 1

Stocks

In [13]:
stocks = {
 'Materials': {'Name': 'Newmont Corporation', 'Ticker': 'NEM'},
 'Communication Services': {'Name': 'Alphabet Inc.', 'Ticker': 'GOOGL'},
 'Consumer Discretionary': {'Name': 'Amazon.com Inc.', 'Ticker': 'AMZN'},
 'Consumer Staples': {'Name': 'PepsiCo Inc.', 'Ticker': 'PEP'},
 'Energy': {'Name': 'National Oilwell Varco Inc.', 'Ticker': 'NOV'},
 'Financial Services': {'Name': 'Bank of America Corp', 'Ticker': 'BAC'},
 'Healthcare': {'Name': 'HCA Healthcare', 'Ticker': 'HCA'},
 'Industrials': {'Name': 'Boeing Company', 'Ticker': 'BA'},
 'Real Estate': {'Name': 'Host Hotels & Resorts', 'Ticker': 'HST'},
 'Information Technology': {'Name': 'Apple Inc.', 'Ticker': 'AAPL'},
 'Utilities': {'Name': 'American Electric Power', 'Ticker': 'AEP'}
}

Downloading data

In [93]:
data = yf.download(
        tickers = [stocks[stock]['Ticker'] for stock in stocks],
        group_by = 'ticker',
        start="2018-06-30"
    )
[*********************100%***********************]  11 of 11 completed
In [94]:
market = yf.download(
    tickers = "^GSPC",
    start="2018-06-30"
)

market.dropna(inplace=True)
market['Returns'] = market.Open.pct_change()
market['Logs'] = np.log(market.Open) - np.log(market.Open.shift(1))
market.dropna(inplace=True)
[*********************100%***********************]  1 of 1 completed
In [96]:
stocks_name = []
for ticks in data.columns.levels[0]:
    for stock in stocks:
        if ticks == stocks[stock]['Ticker']:
            stocks_name.append(stocks[stock]['Name'])
In [97]:
data.columns.set_levels(stocks_name, level=0, inplace=True)
In [98]:
data['Apple Inc.']
Out[98]:
Open High Low Close Adj Close Volume
Date
2018-06-29 46.572498 46.797501 45.727501 46.277500 44.964947 90950800.0
2018-07-02 45.955002 46.825001 45.855000 46.794998 45.467773 70925200.0
2018-07-03 46.947498 46.987499 45.884998 45.980000 44.675892 55819200.0
2018-07-05 46.314999 46.602501 46.070000 46.349998 45.035393 66416800.0
2018-07-06 46.355000 47.107498 46.299999 46.992500 45.659668 69940800.0
... ... ... ... ... ... ...
2020-10-14 121.000000 123.029999 119.620003 121.190002 121.190002 151062300.0
2020-10-15 118.720001 121.199997 118.150002 120.709999 120.709999 112559200.0
2020-10-16 121.279999 121.550003 118.809998 119.019997 119.019997 115393800.0
2020-10-19 119.959999 120.419998 115.660004 115.980003 115.980003 120221600.0
2020-10-20 116.199997 117.620003 115.629997 117.035004 117.035004 50574608.0

583 rows × 6 columns

Statistics about stocks

In [99]:
data.dropna(inplace=True)
In [100]:
for name in stocks_name: 
    data[name, 'Returns'] = data[name].Open.pct_change()
In [101]:
for name in stocks_name: 
    data[name, 'Logs'] = np.log(data[name].Open) - np.log(data[name].Open.shift(1))
In [102]:
data.sort_index(axis=1, inplace=True)
data.dropna(inplace=True)
In [103]:
data.describe(include='all')
Out[103]:
Alphabet Inc. Amazon.com Inc. American Electric Power Apple Inc. Bank of America Corp Boeing Company HCA Healthcare Host Hotels & Resorts National Oilwell Varco Inc. Newmont Corporation PepsiCo Inc.
Adj Close Close High Logs Low Open Returns Volume Adj Close Close High Logs Low Open Returns Volume Adj Close Close High Logs Low Open Returns Volume Adj Close Close High Logs Low Open Returns Volume Adj Close Close High Logs Low Open Returns Volume Adj Close Close High Logs Low Open Returns Volume Adj Close Close High Logs Low Open Returns Volume Adj Close Close High Logs Low Open Returns Volume Adj Close Close High Logs Low Open Returns Volume Adj Close Close High Logs Low Open Returns Volume Adj Close Close High Logs Low Open Returns Volume
count 581.000000 581.000000 581.000000 581.000000 581.000000 581.000000 581.000000 5.810000e+02 581.000000 581.000000 581.000000 581.000000 581.000000 581.000000 581.000000 5.810000e+02 581.000000 581.000000 581.000000 581.000000 581.000000 581.000000 581.000000 5.810000e+02 581.000000 581.000000 581.000000 581.000000 581.000000 581.000000 581.000000 5.810000e+02 581.000000 581.000000 581.000000 581.000000 581.000000 581.000000 581.000000 5.810000e+02 581.000000 581.000000 581.000000 581.000000 581.000000 581.000000 581.000000 5.810000e+02 581.000000 581.000000 581.000000 581.000000 581.000000 581.000000 581.000000 5.810000e+02 581.000000 581.000000 581.000000 581.000000 581.000000 581.000000 581.000000 5.810000e+02 581.000000 581.000000 581.000000 581.000000 581.000000 581.000000 581.000000 5.810000e+02 581.000000 581.000000 581.000000 581.000000 581.000000 581.000000 581.000000 5.810000e+02 581.000000 581.000000 581.000000 581.000000 581.000000 581.000000 581.000000 5.810000e+02
mean 1260.372443 1260.372443 1273.163596 0.000510 1246.262222 1259.675542 0.000695 1.815743e+06 2054.590912 2054.590912 2078.704905 0.001083 2028.485181 2054.865044 0.001354 4.774614e+06 81.152198 83.883167 84.641222 0.000491 82.983735 83.833993 0.000646 2.722699e+06 63.954200 64.588124 65.327233 0.001574 63.755960 64.523171 0.001834 1.360824e+08 27.346732 28.160723 28.484148 -0.000339 27.836282 28.155749 -0.000027 6.235019e+07 297.259135 302.532687 306.884338 -0.001196 298.349673 302.874905 -0.000424 1.367643e+07 125.682360 126.794948 128.553597 0.000459 125.039449 126.831635 0.000869 1.944217e+06 15.739746 16.436816 16.653744 -0.001131 16.224114 16.457435 -0.000761 8.488158e+06 23.628487 23.870628 24.316627 -0.002712 23.478657 23.926368 -0.001927 4.228864e+06 41.982232 42.908227 43.533425 0.000883 42.241084 42.888193 0.001246 8.069566e+06 123.093648 127.063253 128.130181 0.000447 125.901446 127.041050 0.000538 4.883975e+06
std 154.180685 154.180685 155.047071 0.019249 153.048006 153.854745 0.019146 8.530190e+05 500.235552 500.235552 510.003314 0.023258 490.686025 501.422215 0.023233 2.195022e+06 9.036050 8.361387 8.386999 0.017715 8.410890 8.361812 0.017510 1.377182e+06 22.483937 22.179359 22.592153 0.022809 21.727488 22.230114 0.022765 6.490589e+07 3.289198 3.500213 3.401123 0.025041 3.591342 3.500236 0.024928 2.638335e+07 86.927168 90.252341 89.486707 0.039545 90.282787 89.689113 0.039092 1.726442e+07 14.347208 14.640973 14.091016 0.028608 15.063150 14.616630 0.028851 1.043435e+06 3.015098 3.497822 3.449663 0.027217 3.560729 3.498797 0.027240 4.585852e+06 10.325420 10.548820 10.618563 0.039971 10.503366 10.571513 0.039006 1.951559e+06 12.380032 11.902095 12.162012 0.027186 11.617894 11.930430 0.026650 5.151205e+06 12.437766 10.851101 10.829715 0.013516 10.840433 10.856571 0.013403 2.370325e+06
min 984.669983 984.669983 1012.119995 -0.106093 977.659973 984.320007 -0.100659 6.401000e+05 1343.959961 1343.959961 1396.030029 -0.110622 1307.000000 1346.000000 -0.104723 8.813000e+05 63.994541 68.870003 69.239998 -0.130870 65.139999 68.639999 -0.122668 4.401000e+05 34.781353 35.547501 36.430000 -0.090584 35.500000 35.994999 -0.086602 3.247000e+07 17.830576 18.080000 19.670000 -0.136059 17.950001 19.260000 -0.127208 1.380540e+07 95.010002 95.010002 103.570000 -0.328646 89.000000 98.750000 -0.280102 1.390900e+06 68.129997 68.129997 74.209999 -0.167444 58.380001 67.000000 -0.154176 2.445570e+05 9.060000 9.060000 9.700000 -0.167564 7.860000 9.130000 -0.154278 1.190668e+06 8.470000 8.470000 8.850000 -0.398592 8.000000 8.190000 -0.328736 1.041500e+06 27.793959 29.600000 29.830000 -0.289208 29.059999 29.360001 -0.251144 1.772752e+06 99.063225 103.930000 105.760002 -0.109277 101.419998 105.199997 -0.103518 8.118570e+05
25% 1140.910034 1140.910034 1150.050049 -0.008367 1123.709961 1136.400024 -0.008332 1.276000e+06 1761.829956 1761.829956 1777.000000 -0.009883 1742.530029 1760.050049 -0.009834 3.148800e+06 74.494080 77.750000 78.559998 -0.005887 76.690002 77.489998 -0.005870 2.014200e+06 48.228920 48.922501 49.662498 -0.008848 48.447498 49.077499 -0.008809 9.308720e+07 25.209999 25.500000 25.830000 -0.010411 25.100000 25.500000 -0.010357 4.521910e+07 189.080002 189.080002 193.229996 -0.012613 181.899994 188.020004 -0.012534 3.292200e+06 120.180000 120.870003 122.550003 -0.009563 119.120003 120.870003 -0.009518 1.215100e+06 12.310000 12.450000 12.830000 -0.011508 11.990000 12.480000 -0.011442 5.512300e+06 13.000000 13.000000 13.290000 -0.019180 12.690000 13.080000 -0.018997 2.907700e+06 32.292770 33.840000 34.240002 -0.011052 33.360001 33.779999 -0.010992 5.186200e+06 110.133118 115.860001 116.709999 -0.005216 114.879997 115.849998 -0.005202 3.553600e+06
50% 1218.199951 1218.199951 1229.989990 0.001705 1209.040039 1220.000000 0.001706 1.582500e+06 1863.609985 1863.609985 1880.050049 0.001885 1845.640015 1860.000000 0.001886 4.231700e+06 81.492111 83.610001 84.639999 0.001058 82.790001 83.639999 0.001059 2.402200e+06 54.432430 55.537498 55.927502 0.003343 54.787498 55.345001 0.003348 1.195616e+08 27.343346 28.410000 28.700001 0.000796 28.049999 28.379999 0.000796 5.501070e+07 336.279999 344.619995 347.720001 -0.001089 339.500000 344.510010 -0.001088 5.110400e+06 128.281433 129.750000 131.589996 0.001190 128.320007 130.000000 0.001190 1.649800e+06 16.684502 17.459999 17.590000 0.000000 17.270000 17.459999 0.000000 7.508100e+06 22.492393 22.650000 22.980000 -0.002667 22.190001 22.540001 -0.002663 3.772900e+06 37.728992 38.310001 38.660000 0.001512 37.830002 38.250000 0.001513 6.958000e+06 128.079025 131.130005 132.369995 0.000581 129.949997 131.339996 0.000581 4.305600e+06
75% 1384.339966 1384.339966 1398.000000 0.010902 1354.199951 1376.790039 0.010961 2.079200e+06 2049.669922 2049.669922 2059.800049 0.012528 2024.800049 2041.989990 0.012607 5.783400e+06 87.892540 90.430000 91.180000 0.008471 89.650002 90.510002 0.008507 3.103100e+06 75.593697 75.934998 76.292503 0.011572 75.052498 75.805000 0.011639 1.629584e+08 29.149031 30.280001 30.620001 0.009548 30.100000 30.240000 0.009593 7.232570e+07 355.196686 364.010010 368.480011 0.011763 360.010010 364.369995 0.011833 1.824750e+07 135.229996 136.220001 138.279999 0.011765 134.570007 136.360001 0.011835 2.353800e+06 17.919456 19.000000 19.200001 0.009274 18.799999 19.000000 0.009317 9.987300e+06 28.112226 28.490000 28.950001 0.014642 28.160000 28.440001 0.014749 5.000500e+06 48.606682 49.139999 50.290001 0.013501 47.750000 48.880001 0.013593 9.455300e+06 133.061005 135.860001 136.940002 0.007050 134.779999 136.070007 0.007075 5.528000e+06
max 1717.390015 1717.390015 1726.099976 0.075280 1660.189941 1699.520020 0.078186 6.658900e+06 3531.449951 3531.449951 3552.250000 0.099056 3486.689941 3547.000000 0.104128 1.556730e+07 102.555588 104.330002 104.970001 0.082385 103.860001 104.470001 0.085874 2.245570e+07 134.179993 134.179993 137.979996 0.092812 130.529999 137.589996 0.097256 4.268848e+08 34.925472 35.639999 35.720001 0.118690 35.290001 35.700001 0.126021 1.818985e+08 430.299988 440.619995 446.010010 0.225106 440.190002 446.010010 0.252456 1.032128e+08 150.530045 151.039993 151.970001 0.240309 149.500000 151.639999 0.271642 7.008100e+06 20.303028 21.940001 22.059999 0.141136 21.570000 21.940001 0.151581 3.428470e+07 47.846066 48.619999 49.080002 0.217093 48.250000 48.660000 0.242459 1.633560e+07 70.102516 70.370003 72.220001 0.166293 69.779999 71.760002 0.180919 8.254290e+07 143.845245 146.990005 147.199997 0.050685 145.940002 147.110001 0.051992 2.755970e+07

Normal returns

In [104]:
stats = pd.DataFrame(
    {
        'Std': [data[name].Returns.std() for name in stocks_name],
        'Annual Std': [data[name].Returns.std()* np.sqrt(252) for name in stocks_name],
        'Mean': [data[name].Returns.mean() for name in stocks_name],
        'Median': [np.median(data[name].Returns) for name in stocks_name],
        'Min': [data[name].Returns.min() for name in stocks_name],
        'Max': [data[name].Returns.max() for name in stocks_name],
        'Kurtosis': [data[name].Returns.kurtosis() for name in stocks_name],
        'Skewness': [data[name].Returns.skew() for name in stocks_name],
        'Alpha': [linregress(data[name].Returns, market.Returns).intercept for name in stocks_name],
        'Beta': [linregress(data[name].Returns, market.Returns).slope for name in stocks_name],
        'VaR 95% HS': [data[name].Returns.sort_values(ascending=True).quantile(0.05) for name in stocks_name],
        'VaR 95% DN': [norm.ppf(1-0.95, data[name].Returns.mean(), data[name].Returns.std()) for name in stocks_name],
        'Systemic Risk': [linregress(data[name].Returns, market.Returns).slope**2 * market.Returns.var() for name in stocks_name],
    },
    index=[name for name in stocks_name]
).round(6)
In [105]:
market_stats = pd.DataFrame(
    {
        'Std': [market.Returns.std()],
        'Annual Std': [market.Returns.std()* np.sqrt(252)],
        'Mean': [market.Returns.mean()],
        'Median': [np.median(market.Returns.std())],
        'Min': [market.Returns.min()],
        'Max': [market.Returns.max()],
        'Kurtosis': [market.Returns.kurtosis()],
        'Skewness': [market.Returns.skew()],
        'Alpha': [linregress(market.Returns, market.Returns).intercept],
        'Beta': [linregress(market.Returns, market.Returns).slope],
        'VaR 95% HS': [market.Returns.sort_values(ascending=True).quantile(0.05)],
        'VaR 95% DN': [norm.ppf(1-0.95, market.Returns.mean(), market.Returns.std())],
        'Systemic Risk': [linregress(market.Returns, market.Returns).slope**2 * market.Returns.var()]
    },
    index=['S&P500']
).round(6)
In [106]:
market_stats
Out[106]:
Std Annual Std Mean Median Min Max Kurtosis Skewness Alpha Beta VaR 95% HS VaR 95% DN Systemic Risk
S&P500 0.012085 0.191836 0.000473 0.012085 -0.06892 0.0622 5.460796 -0.620362 0.0 1.0 -0.021092 -0.019405 0.000146
In [107]:
stats
Out[107]:
Std Annual Std Mean Median Min Max Kurtosis Skewness Alpha Beta VaR 95% HS VaR 95% DN Systemic Risk
Alphabet Inc. 0.019146 0.303929 0.000695 0.001706 -0.100659 0.078186 3.529431 -0.530836 0.000155 0.457590 -0.029594 -0.030797 0.000031
Amazon.com Inc. 0.023233 0.368814 0.001354 0.001886 -0.104723 0.104128 2.944699 -0.088466 0.000040 0.319555 -0.034528 -0.036861 0.000015
Newmont Corporation 0.026650 0.423063 0.001246 0.001513 -0.251144 0.180919 19.480094 -0.997934 0.000366 0.085740 -0.035136 -0.042590 0.000001
Apple Inc. 0.022765 0.361389 0.001834 0.003348 -0.086602 0.097256 2.888932 -0.215759 -0.000217 0.376052 -0.036681 -0.035611 0.000021
HCA Healthcare 0.028851 0.457994 0.000869 0.001190 -0.154176 0.271642 17.795783 1.000437 0.000262 0.241879 -0.038681 -0.046586 0.000009
American Electric Power 0.017510 0.277966 0.000646 0.001059 -0.122668 0.085874 11.474980 -1.185677 0.000293 0.278190 -0.021319 -0.028155 0.000011
Bank of America Corp 0.024928 0.395720 -0.000027 0.000796 -0.127208 0.126021 6.566391 -0.147686 0.000482 0.327078 -0.037094 -0.041030 0.000016
National Oilwell Varco Inc. 0.039006 0.619202 -0.001927 -0.002663 -0.328736 0.242459 12.309240 -0.533339 0.000785 0.162241 -0.059787 -0.066086 0.000004
Host Hotels & Resorts 0.027240 0.432427 -0.000761 0.000000 -0.154278 0.151581 7.952797 0.382663 0.000662 0.248545 -0.039648 -0.045568 0.000009
PepsiCo Inc. 0.013403 0.212772 0.000538 0.000581 -0.103518 0.051992 9.409430 -1.192622 0.000193 0.520040 -0.019588 -0.021508 0.000039
Boeing Company 0.039092 0.620574 -0.000424 -0.001088 -0.280102 0.252456 15.555435 0.111342 0.000551 0.185897 -0.053918 -0.064725 0.000005

Log Returns

In [108]:
market_stats_logs = pd.DataFrame(
    {
        'Std': [market.Logs.std()],
        'Annual Std': [market.Logs.std()* np.sqrt(252)],
        'Mean': [market.Logs.mean()],
        'Median': [np.median(market.Logs.std())],
        'Min': [market.Logs.min()],
        'Max': [market.Logs.max()],
        'Kurtosis': [market.Logs.kurtosis()],
        'Skewness': [market.Logs.skew()],
        'Alpha': [linregress(market.Logs, market.Logs).intercept],
        'Beta': [linregress(market.Logs, market.Logs).slope],
        'VaR 95% HS': [market.Logs.sort_values(ascending=True).quantile(0.05)],
        'VaR 95% DN': [norm.ppf(1-0.95, market.Logs.mean(), market.Logs.std())],
        'Systemic Risk': [linregress(market.Logs, market.Logs).slope**2 * market.Logs.var()]
    },
    index=['S&P500']
).round(6)
In [109]:
stats_logs = pd.DataFrame(
    {
        'Std': [data[name].Logs.std() for name in stocks_name],
        'Annual Std': [data[name].Logs.std()* np.sqrt(252) for name in stocks_name],
        'Mean': [data[name].Logs.mean() for name in stocks_name],
        'Median': [np.median(data[name].Logs) for name in stocks_name],
        'Min': [data[name].Logs.min() for name in stocks_name],
        'Max': [data[name].Logs.max() for name in stocks_name],
        'Kurtosis': [data[name].Logs.kurtosis() for name in stocks_name],
        'Skewness': [data[name].Logs.skew() for name in stocks_name],
        'Alpha': [linregress(data[name].Logs, market.Logs).intercept for name in stocks_name],
        'Beta': [linregress(data[name].Logs, market.Logs).slope for name in stocks_name],
        'VaR 95% HS': [data[name].Logs.sort_values(ascending=True).quantile(0.05) for name in stocks_name],
        'VaR 95% DN': [norm.ppf(1-0.95, data[name].Logs.mean(), data[name].Logs.std()) for name in stocks_name],
        'Systemic Risk': [linregress(data[name].Logs, market.Logs).slope**2 * market.Logs.var() for name in stocks_name],
    },
    index=[name for name in stocks_name]
).round(6)
In [110]:
market_stats_logs
Out[110]:
Std Annual Std Mean Median Min Max Kurtosis Skewness Alpha Beta VaR 95% HS VaR 95% DN Systemic Risk
S&P500 0.01213 0.192566 0.000399 0.01213 -0.07141 0.060343 5.647346 -0.748376 0.0 1.0 -0.021318 -0.019554 0.000147
In [111]:
stats_logs
Out[111]:
Std Annual Std Mean Median Min Max Kurtosis Skewness Alpha Beta VaR 95% HS VaR 95% DN Systemic Risk
Alphabet Inc. 0.019249 0.305575 0.000510 0.001705 -0.106093 0.075280 3.827091 -0.683230 0.000165 0.459344 -0.030040 -0.031152 0.000031
Amazon.com Inc. 0.023258 0.369209 0.001083 0.001885 -0.110622 0.099056 3.068223 -0.260859 0.000049 0.323795 -0.035138 -0.037172 0.000015
Newmont Corporation 0.027186 0.431562 0.000883 0.001512 -0.289208 0.166293 26.732727 -1.925516 0.000321 0.089019 -0.035768 -0.043834 0.000001
Apple Inc. 0.022809 0.362086 0.001574 0.003343 -0.090584 0.092812 2.926319 -0.379213 -0.000195 0.377863 -0.037370 -0.035944 0.000021
HCA Healthcare 0.028608 0.454143 0.000459 0.001190 -0.167444 0.240309 13.882951 0.279200 0.000286 0.246323 -0.039449 -0.046598 0.000009
American Electric Power 0.017715 0.281221 0.000491 0.001058 -0.130870 0.082385 12.823261 -1.511093 0.000262 0.279586 -0.021549 -0.028648 0.000012
Bank of America Corp 0.025041 0.397507 -0.000339 0.000796 -0.136059 0.118690 6.797702 -0.466993 0.000511 0.328444 -0.037799 -0.041527 0.000016
National Oilwell Varco Inc. 0.039971 0.634523 -0.002712 -0.002667 -0.398592 0.217093 19.195378 -1.517227 0.000828 0.158151 -0.061649 -0.068459 0.000004
Host Hotels & Resorts 0.027217 0.432060 -0.001131 0.000000 -0.167564 0.141136 8.109270 -0.021272 0.000683 0.250570 -0.040455 -0.045900 0.000009
PepsiCo Inc. 0.013516 0.214566 0.000447 0.000581 -0.109277 0.050685 10.729987 -1.402589 0.000167 0.520128 -0.019783 -0.021785 0.000040
Boeing Company 0.039545 0.627750 -0.001196 -0.001089 -0.328646 0.225106 17.959496 -0.963642 0.000619 0.183380 -0.055426 -0.066241 0.000005

Apple Analysis

In [112]:
fig = px.line(x=data['Apple Inc.'].index, y=data['Apple Inc.'].Close)
fig.update_xaxes(rangeslider_visible=True)

fig.update_layout(
    width=1200, 
    height=600,
    title='Apple Analysis during the COVID',
    yaxis_title='Closing Price',
    xaxis_title='Date',
    shapes = [dict(
        x0='2020-02-15', x1='2020-02-15', y0=0, y1=1, xref='x', yref='paper',
        line_width=2)],
    annotations=[dict(
        x='2020-02-17', y=0.95, xref='x', yref='paper',
        showarrow=False, xanchor='left', text='COVID Begins')],
    yaxis=dict(
    ticksuffix=' $'
    ),
)

pyo.iplot(fig, filename='jupyter-basic_bar')

Amazon Analysis

In [116]:
fig = go.Figure(data=[go.Candlestick(x=data['Amazon.com Inc.'].index,
                open=data['Amazon.com Inc.'].Open,
                high=data['Amazon.com Inc.'].High,
                low=data['Amazon.com Inc.'].Low,
                close=data['Amazon.com Inc.'].Close)])

fig.update_layout(
    width=1200, 
    height=800,
    title='Amazon.com Analysis during the COVID',
    yaxis_title='Prices',
    shapes = [dict(
        x0='2020-02-15', x1='2020-02-15', y0=0, y1=1, xref='x', yref='paper',
        line_width=2)],
    annotations=[dict(
        x='2020-02-17', y=0.95, xref='x', yref='paper',
        showarrow=False, xanchor='left', text='COVID Begins')],
    yaxis=dict(
    ticksuffix=' $'
    ),
)

pyo.iplot(fig, filename='test')

Boeing Company Analysis

In [117]:
colors = np.where(data['Boeing Company'].Returns < 0, 'red', 'green')

# Create figure with secondary y-axis
fig = make_subplots(specs=[[{"secondary_y": True}]])

# Add traces
fig.add_trace(
    go.Scatter(x=data['Boeing Company'].index, y=data['Boeing Company'].Close, name="Closing Prices", yaxis="y"),
    secondary_y=False
)

fig.add_trace(
    go.Bar(x=data['Boeing Company'].index, y=(data['Boeing Company'].Returns * 100), name="Returns", marker_color=colors, yaxis="y1"),
    secondary_y=True
)

# Set x-axis title
fig.update_xaxes(title_text="Date")

# Set y-axes titles
# fig.update_yaxes(title_text="<b>primary</b> yaxis title", secondary_y=False)
# fig.update_yaxes(title_text="<b>secondary</b> yaxis title", secondary_y=True)

fig.update_layout(
    width=1200, 
    height=600,
    title='Boeing Company Analysis during the COVID',
    yaxis=dict(
    title="TSLA Close's Prices",
    ticksuffix=' $'

    ),
    yaxis2=dict(
        title="TSLA Returns",
        ticksuffix = '%'
    ),
    shapes = [dict(
        x0='2020-02-15', x1='2020-02-15', y0=0, y1=1, xref='x', yref='paper',
        line_width=2)],
    annotations=[dict(
        x='2020-02-17', y=0.95, xref='x', yref='paper',
        showarrow=False, xanchor='left', text='COVID Begins')]
)

pyo.iplot(fig)

Compare Stocks

In [183]:
fig = go.Figure()

visible = [False] * len(stocks_name)
visible[0] = True

for i, name in enumerate(stocks_name):
    fig.add_trace(
        go.Scatter(
            x = data[name].index,
            y = data[name].Close,
            name = name,
            visible=visible[i]
        )
    )

buttons = []

for i, name in enumerate(stocks_name):
    false_true = [False] * len(stocks_name)
    false_true[i] = True
    buttons.append(
        dict(label = name,
                method = 'update',
                args = [{'visible': false_true}])
    )

fig.update_layout(

    updatemenus=[
        dict(buttons=buttons,
        direction="down",
        pad={"r": 10, "t": 10},
        x=0.8,
        xanchor="left",
        y=1.2,
        yanchor="top",
        active=0,
        )],
)


fig.update_layout(
    width=1200, 
    height=600,
    title='Two years in S&P500',
    yaxis_title='Closing Prices',
    shapes = [dict(
        x0='2020-02-15', x1='2020-02-15', y0=0, y1=1, xref='x', yref='paper',
        line_width=2)],

    annotations=[
        dict(x='2020-02-17', y=0.95, xref='x', yref='paper',
        showarrow=False, xanchor='left', text='COVID Begins'),
        
        dict(text="Choose Stocks:", showarrow=False,x=0.66,
        xanchor='left', y=1.17, yanchor="top",
        yref='paper', xref='paper',
        font=dict(
            # family="Courier New, monospace",
            size=18,
            color="Black")
        )],

    yaxis=dict(
    ticksuffix=' $'
    ),
)


pyo.iplot(fig)
In [ ]: